﻿using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.IO;
using System.Linq;
using DeepZoomPivotConstructor.PivotLib;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PaperRenderingInterfaces;
using t_PivotGeneratorTestHelpers;
using t_TestingLibrary;
using TwikiLinkScanners;

namespace t_TwikiLinkScanners
{
    [TestClass]
    public class t_ATLASMainPhysicsResult
    {
        [TestInitialize]
        public void TestSetup()
        {
            Utilities.MyClassInit();
        }

        [TestCleanup]
        public void TestCleanup()
        {
            Utilities.MyClassDone();
        }

        [TestMethod]
        public void TestCTor()
        {
            var target = new ATLASMainPhysicsResults();
        }

        /// <summary>
        /// Helper class that doesn't actually do any rendering as much as hold onto things.
        /// </summary>
        [Export(typeof(IPaperRenderEngine))]
        class DummyRender : IPaperRenderEngine
        {
            public List<string> addressesRequested = new List<string>();

            private Dictionary<string, int> requestTypeCount = new Dictionary<string, int>();

            public IEnumerable<PivotImage> Render(string renderName, string renderAddress)
            {
                Trace.WriteLine("Rendering " + renderName + " - " + renderAddress);
                string toCache = renderName + "-" + renderAddress;
                if (!addressesRequested.Contains(toCache))
                {
                    addressesRequested.Add(toCache);
                    PivotImage pm = new PivotImage(null);
                    pm.Name = "HSF1";
                    if (!requestTypeCount.Keys.Contains(renderName))
                    {
                        requestTypeCount[renderName] = 0;
                    }
                    requestTypeCount[renderName] += 1;

                    if (SetSubTitle != "" && SetSubTitle != null)
                    {
                        pm.Add("Note Title", SetSubTitle);
                    }
                    yield return pm;
                }
            }

            public int TypeRequestCount(string name)
            {
                if (!requestTypeCount.Keys.Contains(name))
                {
                    return 0;
                }
                return requestTypeCount[name];
            }

            public string SetSubTitle { get; set; }
        }

        [TestMethod]
        public void TestATLASPage()
        {
            var lf = new LoadAFile() { theFileToLoad = new FileInfo("..\\..\\..\\t_TwikiLinkScanners\\MainATLASPage.html") };
            Utilities.AddPart(lf);
            var dr = new DummyRender();
            Utilities.AddPart(dr);
            Utilities.AddPart(new t_TestingLibrary.TestLogger());
            var target = new ATLASMainPhysicsResults();
            Utilities.Compose(target);

            var output = target.Render("https://twiki.cern.ch/twiki/bin/view/AtlasPublic");
            Assert.IsNotNull(output, "Output should not be null");
            var results = output.ToArray();
            Assert.AreEqual(56, dr.TypeRequestCount("HTMLSimpleTables"), "Expected # of do me now's to be 1!");
            Assert.AreEqual(56, dr.TypeRequestCount("TwikSubLinkScanner"), "Expected # of do me now's to be 1!");
            Assert.AreEqual(56, dr.TypeRequestCount("ATLASMasterConfNotePage"), "Expected # of do me now's to be 1!");
            Assert.AreEqual(56 * 3, results.Length, "Expected a single link to be found!");

            Dictionary<string, int> counters = new Dictionary<string, int>();
            foreach (var item in results)
            {
                var grpName = item.GetMetadata<PivotMetadataString>("Group").FirstOrDefault().Value;
                if (!counters.ContainsKey(grpName))
                {
                    counters[grpName] = 1;
                }
                else
                {
                    counters[grpName] += 1;
                }
            }

            Assert.AreEqual(9, counters["Event Displays"], "Expected 3 links pointing to event displays");

        }
    }
}
